﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using JM.JCY.BusinessEntity;
using JM.JCY.Proxy;
using NCS.IConnect.ApplicationContexts;
using System.Web.UI.HtmlControls;
using Microsoft.SharePoint;
using System.Data;

namespace JM.JCY.SharePointComponent.UserManagement.RoleMgrDetails
{
    public partial class RoleMgrDetailsUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["Id"] != null)
                {
                    using (ApplicationProxy proxy = new ApplicationProxy())
                    {

                        ApplicationDataSet.T_ApplicationDataTable dtApp = proxy.GetApplicationNameById(Int32.Parse(Request.QueryString["Id"].ToString()));
                        this.lblAppName.Text = dtApp.Rows[0]["ApplicationName"].ToString();
                    }
                }

                using (UserManagementProxy userProxy = new UserManagementProxy())
                {
                    // load for member list box
                    DataSetUserManagement.T_UserInfoDataTable userDT = userProxy.GetUser(0, string.Empty);

                    ApplicationProxy appProxy = new ApplicationProxy();
                    ApplicationDataSet.T_ApplicationRoleDataTable appRoleDT = appProxy.GetApplicationRoleByAppId(Int32.Parse(Request.QueryString["Id"].ToString()));



                    if (appRoleDT.Rows.Count == 1)
                    {
                        if (appRoleDT.Rows[0]["DefaultRole"].ToString() == "Y")
                        {
                            this.rblApplication.SelectedValue = "0";
                            this.tbListBox.Visible = false;
                        }
                        else
                        {
                            this.rblApplication.SelectedValue = "1";
                            this.tbListBox.Visible = true;
                            foreach (ApplicationDataSet.T_ApplicationRoleRow existUser in appRoleDT.Rows)
                            {
                                for (int i = 0; i < userDT.Rows.Count; i++)
                                {
                                    if (userDT[i]["ID"].Equals(existUser.UserId))
                                    {
                                        userDT.Rows.Remove(userDT[i]);
                                    }
                                }
                            }

                            if (userDT.Rows.Count > 0)
                            {
                                box1View.DataSource = userDT;
                                box1View.DataTextField = "UserName";
                                box1View.DataValueField = "ID";
                                box1View.DataBind();
                            }

                            // bind box view for current members
                            box2View.DataSource = appRoleDT;
                            box2View.DataTextField = "UserName";
                            box2View.DataValueField = "UserId";
                            box2View.DataBind();
                        }
                    }


                    if (appRoleDT.Rows.Count > 1)
                    {
                        this.rblApplication.SelectedValue = "1";
                        this.tbListBox.Visible = true;
                        foreach (ApplicationDataSet.T_ApplicationRoleRow existUser in appRoleDT.Rows)
                        {
                            for (int i = 0; i < userDT.Rows.Count; i++)
                            {
                                if (userDT[i]["ID"].Equals(existUser.UserId))
                                {
                                    userDT.Rows.Remove(userDT[i]);
                                }
                            }
                        }

                        if (userDT.Rows.Count > 0)
                        {
                            box1View.DataSource = userDT;
                            box1View.DataTextField = "UserName";
                            box1View.DataValueField = "ID";
                            box1View.DataBind();
                        }

                        // bind box view for current members
                        box2View.DataSource = appRoleDT;
                        box2View.DataTextField = "UserName";
                        box2View.DataValueField = "UserId";
                        box2View.DataBind();

                    }
                }

            }
        }

        protected void rblApplication_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.rblApplication.SelectedValue == "1")
            {
                this.tbListBox.Visible = true;

                using (UserManagementProxy userProxy = new UserManagementProxy())
                {
                    // load for member list box
                    DataSetUserManagement.T_UserInfoDataTable userDT = userProxy.GetUser(0, string.Empty);

                    ApplicationProxy appProxy = new ApplicationProxy();
                    ApplicationDataSet.T_ApplicationRoleDataTable appRoleDT = appProxy.GetApplicationRoleByAppId(Int32.Parse(Request.QueryString["Id"].ToString()));



                    if (appRoleDT.Rows.Count == 1)
                    {
                        if (appRoleDT.Rows[0]["DefaultRole"].ToString() == "Y")
                        {
                            box1View.DataSource = userDT;
                            box1View.DataTextField = "UserName";
                            box1View.DataValueField = "ID";
                            box1View.DataBind();
                        }
                        else
                        {
                           
                          
                            foreach (ApplicationDataSet.T_ApplicationRoleRow existUser in appRoleDT.Rows)
                            {
                                for (int i = 0; i < userDT.Rows.Count; i++)
                                {
                                    if (userDT[i]["ID"].Equals(existUser.UserId))
                                    {
                                        userDT.Rows.Remove(userDT[i]);
                                    }
                                }
                            }

                            if (userDT.Rows.Count > 0)
                            {
                                box1View.DataSource = userDT;
                                box1View.DataTextField = "UserName";
                                box1View.DataValueField = "ID";
                                box1View.DataBind();
                            }

                            // bind box view for current members
                            box2View.DataSource = appRoleDT;
                            box2View.DataTextField = "UserName";
                            box2View.DataValueField = "UserId";
                            box2View.DataBind();
                        }
                    }


                    if (appRoleDT.Rows.Count > 1)
                    {
                      
                      
                        foreach (ApplicationDataSet.T_ApplicationRoleRow existUser in appRoleDT.Rows)
                        {
                            for (int i = 0; i < userDT.Rows.Count; i++)
                            {
                                if (userDT[i]["ID"].Equals(existUser.UserId))
                                {
                                    userDT.Rows.Remove(userDT[i]);
                                }
                            }
                        }

                        if (userDT.Rows.Count > 0)
                        {
                            box1View.DataSource = userDT;
                            box1View.DataTextField = "UserName";
                            box1View.DataValueField = "ID";
                            box1View.DataBind();
                        }

                        // bind box view for current members
                        box2View.DataSource = appRoleDT;
                        box2View.DataTextField = "UserName";
                        box2View.DataValueField = "UserId";
                        box2View.DataBind();

                    }
                  
                }
            }
            else
            {
                this.tbListBox.Visible = false;
            }

        

        }

        protected void imgSave_Click(object sender, ImageClickEventArgs e)
        {
            using (ApplicationProxy appProxy = new ApplicationProxy())
            {
                ApplicationDataSet.T_ApplicationRoleDataTable dt = new ApplicationDataSet.T_ApplicationRoleDataTable();

                if (this.rblApplication.SelectedValue == "0")
                {
                    appProxy.DeleteApplicationRoleByAppId(Int32.Parse(Request.QueryString["Id"].ToString()));
                    var drRole = dt.NewT_ApplicationRoleRow();
                    drRole.DefaultRole = "Y";
                    drRole.UserId = 0;
                    drRole.UserName = "admin";
                    drRole.ApplicationId = Int32.Parse(Request.QueryString["Id"].ToString());
                    JCY.Common.JCYUtility.UpdateCommonFields(drRole, "admin");
                    dt.AddT_ApplicationRoleRow(drRole);
                }
                else
                {
                    appProxy.DeleteApplicationRoleByAppId(Int32.Parse(Request.QueryString["Id"].ToString()));

                    string s = Request.Form[box2View.UniqueID];
                    if (!string.IsNullOrEmpty(s))
                    {
                     
                        foreach (var item in s.Split(','))
                        {
                            var drRole = dt.NewT_ApplicationRoleRow();
                            drRole.UserId = Int32.Parse(item);

                            UserManagementProxy userProxy = new UserManagementProxy();
                            DataSetUserManagement.T_UserInfoDataTable userInfoDT = userProxy.GetUser(Int32.Parse(item), "");
                            drRole.DefaultRole = "N";
                            drRole.UserName = userInfoDT.Rows[0]["UserName"].ToString();
                            drRole.ApplicationId = Int32.Parse(Request.QueryString["Id"].ToString());
                            JCY.Common.JCYUtility.UpdateCommonFields(drRole, "admin");
                            dt.AddT_ApplicationRoleRow(drRole);
                        }

                    }
                   
                }

                appProxy.CreateApplicationRole(dt);
           
              

            }

            Response.Redirect("RoleMgr.aspx");
            
        }

        protected void imgCancel_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("RoleMgr.aspx");
        }

      
    }
}
